package common.sendmsg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.configuration.AsyncFlowGetConfiguration;
import common.dateutils.AsyncFlowDateUtil;
import common.http.AsyncFlowHttpClientUtil;
import lombok.Data;
import models.AsyncFlowInfo;
import models.AsyncFlowRequest;
import org.apache.commons.lang.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;

public class AsyncFlowSendMsg {


    /**
     * 发送企业微信
     *
     * @param users   接收人工号，多个接收者用‘|’分隔，最多支持100个
     * @param content 内容，（企业微信最长不超过2048个字节）
     */
    public static void SendQYWXMsg(String users, String content, String sendUrl) {

        if (StringUtils.isBlank(sendUrl)) {
            return;
        }
        SendMsgReqRequest sendMsgReqRequest = new SendMsgReqRequest();
        sendMsgReqRequest.setUsers(users);
        sendMsgReqRequest.setContent(content);
        String result = AsyncFlowHttpClientUtil.post(sendUrl, JSONObject.toJSONString(sendMsgReqRequest));
    }

    /**
     * 发送企业微信
     *
     * @param users   接收人工号，多个接收者用‘|’分隔，最多支持100个
     * @param content 内容，（企业微信最长不超过2048个字节）
     */
    public static void SendQYWXMsg(String users, String content) {

        String sendUrl = new AsyncFlowGetConfiguration().getAlarmUrl();
        if (StringUtils.isBlank(sendUrl)) {
            return;
        }
        SendMsgReqRequest sendMsgReqRequest = new SendMsgReqRequest();
        sendMsgReqRequest.setUsers(users);
        sendMsgReqRequest.setContent(content);
        String result = AsyncFlowHttpClientUtil.post(sendUrl, JSONObject.toJSONString(sendMsgReqRequest));
    }

    /**
     * 发送业务报警
     *
     * @param asyncFlowRequest 请求参数
     * @param resMsg           返回描述
     * @param fileName         文件名
     * @param asyncFlowInfo    报警工号
     */
    public static void sendAlarmMsg(AsyncFlowRequest asyncFlowRequest, String resMsg, String fileName, AsyncFlowInfo asyncFlowInfo) {
        String serialid = asyncFlowRequest.getBusinessSerialId();

        if (StringUtils.isBlank(serialid)) {
            String dateGuid = AsyncFlowDateUtil.htFormatyyyyMMddHHmmV2(asyncFlowRequest.getRequesterTime());
            serialid = asyncFlowRequest.getAsyncFlowNodeEnum().getMsg() + "-" + dateGuid;
        }
        StringBuilder stringBuilder;
        stringBuilder = new StringBuilder();
        stringBuilder.append("【业务报警】")
                .append("\n节点：").append(asyncFlowRequest.getAsyncFlowModuleEnum().getMsg()).append("--").append(asyncFlowRequest.getAsyncFlowNodeEnum().getMsg())
                .append("\n来源：").append(asyncFlowRequest.getSourceDes())
                .append("\n位置：").append(fileName)
                .append("\n单号：").append(serialid)
                .append("\n时间：").append(AsyncFlowDateUtil.htFormat(LocalDateTime.now()))
                .append("\n环境：").append(asyncFlowInfo.getEvn())
                .append("\n错误：").append(resMsg)
                .append("\n请求：").append(JSON.toJSONString(asyncFlowRequest));
        SendQYWXMsg(asyncFlowInfo.getFlowMaintainName(), stringBuilder.toString(), asyncFlowInfo.getAlarmUrl());

    }

    public static String buildErrorMsg(AsyncFlowRequest asyncFlowRequest, Exception exception, String exeName, AsyncFlowInfo asyncFlowInfo) {

        StringBuilder stringBuilder = new StringBuilder();
        String serialid = asyncFlowRequest.getBusinessSerialId();

        if (StringUtils.isBlank(serialid)) {
            String dateGuid = AsyncFlowDateUtil.htFormatyyyyMMddHHmmV2(asyncFlowRequest.getRequesterTime());
            ;
            serialid = asyncFlowRequest.getAsyncFlowNodeEnum().getMsg() + "-" + dateGuid;
        }

        stringBuilder.append("【节点异常报警】")
                .append("\n节点：").append(asyncFlowRequest.getAsyncFlowModuleEnum().getMsg()).append("--").append(asyncFlowRequest.getAsyncFlowNodeEnum().getMsg())
                .append("\n来源：").append(asyncFlowRequest.getSourceDes())
                .append("\n方法：").append(exeName)
                .append("\n单号：").append(serialid)
                .append("\n时间：").append(AsyncFlowDateUtil.htFormat(LocalDateTime.now()))
                .append("\n环境：").append(asyncFlowInfo.getEvn())
                .append("\n异常：").append(exception.toString())
                .append("\n位置：").append(Arrays.toString(exception.getStackTrace()));
        if (stringBuilder.toString().length() > 2048) {
            return stringBuilder.toString().substring(0, 2047);
        }

        return stringBuilder.toString();
    }

    @Data
    static class SendMsgReqRequest {

        //接收人工号，多个接收者用‘|’分隔，最多支持100个
        public String users;

        //内容，（企业微信最长不超过2048个字节，iOA最长不超过250个字）
        public String content;
    }
}
